<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 专辑 model
 *
 */
class Albummodel extends CI_Model {

	function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 取得会员的专辑列表
     *
     * @param unknown_type $uid
     */
    function get_albums($uid='')
    {
    	$this->dblib->set_query_limit();
    	$this->db->select('a.*, count(*) as appcount')
    				->from('album as a')
    				->join('albumapp as aa', 'a.id=aa.albumid', 'left')
    				->where(array('a.uid' => $uid, 'isdel' => 0))
    				->group_by('a.id')
    				->order_by('a.createtime', 'desc');
    	$query = $this->db->get();
    	return $query->result_array();
    }
    
    /**
     * 取得具体一个专辑
     *
     * @param unknown_type $id
     */
    function get_album($id='')
    {
    	$query = $this->db->get_where('album', array('id' => $id, 'isdel' => 0));
    	return $query->result_array();
    }
    
    /**
     * 根据专辑名称取得指定用户的专辑
     *
     * @param unknown_type $uid
     * @param unknown_type $name
     */
    function get_album_by_name($uid='', $name='')
    {
    	$query = $this->db->get_where('album', array('uid' => $uid, 'name' => $name), 1);
    	return $query->row_array();
    }
    
    /**
     * 检测具体的专辑是否属于具体一会员的
     *
     * @param unknown_type $id
     * @param unknown_type $uid
     */
    function check_album($id='', $uid='', $belong_user=1)
    {
    	$where = array('id' => $id, 'isdel' => 0);
    	if($uid && $belong_user){
    		$where['uid'] = $uid;
    	}elseif($uid && !$belong_user){
    		$where['uid <>'] = $uid;
    	}
    	
    	$this->db->select('count(*) as count');
		$this->db->where($where, FALSE);
		$query = $this->db->get('album');
		$row = $query->row_array();
		return $row['count'] > 0;
    }
    
    /**
     * 检测专辑名是否有重复
     *
     * @param unknown_type $name
     */
    function check_album_name($uid='', $name='', $belong_user=1)
    {
    	$where = array('uid' => $uid, 'isdel' => 0);
    	if($name && $belong_user){
    		$where['name'] = $name;
    	}elseif($name && !$belong_user){
    		$where['name <>'] = $name;
    	}
    	
    	$this->db->select('count(*) as count');
		$this->db->where($where, FALSE);
		$query = $this->db->get('album');
		$row = $query->row_array();
		return $row['count'] > 0;
    }
    
    /**
     * 添加专辑
     *
     * @param unknown_type $data
     */
    function insert_album($data=array())
    {
    	if(empty($data) || !isset($data['name']) || empty($data['name']) ||
    			!isset($data['uid']) || empty($data['uid'])){
    				return FALSE;
    			}
    	
    	//生成id
    	$this->db->set('id', '(SELECT UUID())', FALSE);
    	$this->db->set('createtime', '(SELECT NOW())', FALSE);
		return $this->db->insert('album', $data);
    }
    
    /**
     * 给专辑添加成员
     *
     * @param unknown_type $albumid
     * @param unknown_type $appid
     */
    function insert_albumapp($albumid='', $appid='')
    {
    	$this->db->set('createtime', '(SELECT NOW())', FALSE);
    	return $this->db->insert('albumapp', array('albumid' => $albumid, 'appid' => $appid));
    }
    
    /**
     * 检测albumapp中用户的专辑
     *
     * @param unknown_type $uid
     * @param unknown_type $albumid
     */
    function check_albumapp($albumid='', $appid='')
    {
    	$this->db->select('count(*) as count');
		$query = $this->db->get_where('albumapp', array('appid' => $appid, 'albumid' => $albumid));
		$row = $query->row_array();
		return $row['count'] > 0;
    }
    
    /**
     * 删除专辑
     *
     * @param unknown_type $albumid
     */
    function delete_album($albumid='')
    {
    	return $this->db->update('album', array('isdel' => 1), array('id' => $albumid));
    }
    
    /**
     * 设置专辑状态已分享
     *
     * @param unknown_type $appid
     * @param unknown_type $shared
     * @return unknown
     */
    function set_album_shared($albumid='', $shared=1)
    {
    	$this->db->set('sharedtime', '(SELECT NOW())', FALSE);//设置分享时间
    	return $this->db->update('album', array('share' => $shared), array('id' => $albumid));
    }
    
    /**
     * 检测给定的专辑是否以分享
     *
     * @param unknown_type $albumid
     * @param unknown_type $shared
     * @return unknown
     */
    function check_album_shared($albumid='', $shared=1)
    {
    	$this->db->select('count(*) as count');
		$this->db->where(array('share' => $shared, 'id' => $albumid));
		$query = $this->db->get('album');
		$row = $query->row_array();
		return $row['count'] > 0;
    }
    
    /**
     * 取得专辑中的应用
     *
     * @param unknown_type $albumid
     */
    function get_album_apps($albumid='')
    {
    	$result = array();
    	$this->dblib->set_query_limit();//分页
    	$this->db->select('a.id as app_id, a.name as app_name, a.photo as app_photo, a.uid')
    			 ->from('app as a')
    			 ->join('albumapp as aa', 'aa.appid=a.id')
    			 ->where('aa.albumid', $albumid);
    	$query = $this->db->get();
    	if ($query->num_rows() > 0) {
			//如果有结果集
			$this->load->library('uploadimglib');
			$result = $query->result_array();
			//设置photo路径
			foreach ($result as $k=>$v){
				$result[$k]['app_photo'] = $this->uploadimglib->re_image_path($v['app_photo']);
			}
		}
		return $result;
    }
    
    
}


/* End of file albummodel.php */
/* Location: ./application/models/albummodel.php */